//
//  WDMineViewController.m
//  wd-userapp
//
//  Created by Bin Tang on 2022/12/27.
//

#import "WDMineViewController.h"
#import "WDMineViewModel.h"
#import "WDMineViewCell.h"
#import "WDMineHeaderView.h"
#import "WDMineFooterView.h"
#import "WDMineLogoutTipView.h"

@interface WDMineViewController ()
@property (nonatomic, strong) WDMineViewModel *viewModel;

@property (nonatomic, strong) WDMineHeaderView *headerView;

@property (nonatomic, strong) WDMineFooterView *footerView;

@property (nonatomic, strong) WDMineLogoutTipView *logoutTipView;

@end

@implementation WDMineViewController

- (void)initWithVar
{
    [super initWithVar];
    
}
- (void)initWithViewModel
{
    _viewModel = [WDMineViewModel new];
}
- (void)initWithNavBar
{
    [super initWithNavBar];
}
- (void)initWithSubView
{
    [super initWithSubView];
    self.navigationItem.title = @"我的";
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.baseTableView.height = self.baseTableView.height - TABBAR_HEIGHT;
    [self.baseTableView registerClass:[WDMineViewCell class] forCellReuseIdentifier:[WDMineViewCell className]];
    self.baseTableView.backgroundColor = [UIColor whiteColor];
    
    
    [self initWithLogoutTipView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[EDSAppDelegateTool sharedInstance] requestWithUserInfo:^(EDSRespondDataEntity * _Nonnull respond) {
        [self initWithHeaderView];
    }];
}

- (void)initWithHeaderView {
    if (!_headerView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDMineHeaderViewHeight);
        _headerView = [[WDMineHeaderView alloc] initWithFrame:frame];
        self.baseTableView.tableHeaderView = _headerView;
        @weakify(self);
        _headerView.touchHeaderImageViewBlock = ^{
            @strongify(self);
            [self actionWithPersonalSetting];
        };
    }
    
    
    [self.headerView updateWithViewData:self.viewModel];
}

- (void)initWithFooterView {
    CGRect frame = CGRectMake(0, 0, kScreenW, kWDMineFooterViewHeight);
    _footerView = [[WDMineFooterView alloc] initWithFrame:frame];
    self.baseTableView.tableFooterView = self.footerView;
    
    @weakify(self);
    _footerView.viewBlock = ^(id  _Nullable data) {
        @strongify(self);
        [self actionWithShowLogoutTipView];
    };
}

/// 个人设置
- (void)actionWithPersonalSetting {
    UIViewController *vc = [[TYZRouter sharedInstance] getPersonalSettingVC:@{}];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

- (void)actionWithShowLogoutTipView {
    @weakify(self);
    [_logoutTipView showWithTitle:@"退出登录" data:nil completion:^{
        @strongify(self);
        [self.logoutTipView hide];
        [self actionWithLogout];
        } failed:^{
            @strongify(self);
            [self.logoutTipView hide];
        }];
}

- (void)initWithLogoutTipView {
    CGRect frame = CGRectMake((kScreenW - 296) * 0.5, (kScreenH - 176) * 0.5 - 30, 296, 176);
    _logoutTipView = [[WDMineLogoutTipView alloc] initWithFrame:frame];
}

/// 退出登录
- (void)actionWithLogout {
//    @weakify(self);
    [self.viewModel requestWithLogout:^(EDSRespondDataEntity * _Nonnull respond) {
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [SVProgressHUD showSuccessWithStatus:@"退出登录成功！"];
            [[EDSAppDelegateTool sharedInstance] updateRootVC];
        } else {
            [SVProgressHUD showInfoWithStatus:@"退出登录失败，请稍后再试！"];
        }
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kWDMineViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TYZCommonCellModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    WDMineViewCell *cell = [WDMineViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TYZCommonCellModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    UIViewController *vc = nil;
    if ([model.title isEqualToString:@"修改密码"]) {
        vc = [[TYZRouter sharedInstance] getModifyPwdVC:nil];
    } else if ([model.title isEqualToString:@"关于我们"]) {
        vc = [[TYZRouter sharedInstance] getMineAboutVC:nil];
    }
    if (vc) {
        [TYZJumpVCManager pushBaseVC:self pushVC:vc];
    }
}



@end
